package com.hunan.api.catv.service.biz.tencent_cloud.vod;

import com.google.gson.JsonObject;
import com.hunan.api.catv.common.CacheKey;
import com.hunan.api.catv.common.Constants;
import com.hunan.api.catv.common.ConstantsMsg;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.biz.cache.CacheBiz;
import com.hunan.api.catv.service.impl.tencent_cloud.vod.SignatureService;
import com.hunan.api.catv.utils.GsonUtils;
import com.hunan.api.catv.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class SignatureBiz extends ResultService {

    @Autowired
    SignatureService signatureService;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    CacheBiz cacheBiz;

    public ServiceResult sign() {
        String signKey = CacheKey.vod_sign;
        Object signObject = redisUtils.get(signKey);
        if (!ObjectUtils.isEmpty(signObject)) {
            return buildOK(signObject.toString());
        }
        JsonObject vodConf = GsonUtils.fromJson(cacheBiz.getSystemDict(Constants.tencent_cloud_vod_conf), JsonObject.class);
        if (!ObjectUtils.isEmpty(vodConf)) {
            String secretId = vodConf.get("secretId").getAsString();
            String secretKey = vodConf.get("secretKey").getAsString();
            String procedure = vodConf.get("procedure").getAsString();
            String sign = signatureService.getUploadSignature(secretId, secretKey, procedure);
            if (!ObjectUtils.isEmpty(sign)) {
                redisUtils.set(signKey, sign, 3600);
                return buildOK(sign);
            }
        }
        return buildFail(ConstantsMsg.vod_getSign_fail);
    }

}
